package com.phiture.erp.produce.core.handler;

import com.phiture.erp.produce.core.convert.ErpMaterialRequirementPlanConvertMapper;
import com.phiture.erp.produce.core.pojo.dto.MrpTaskDispatchDTO;
import com.phiture.erp.produce.dal.entity.ErpMaterialRequirementPlanItemsDO;
import com.phiture.erp.purchase.api.pojo.req.CreatePurchaseRequestFromMrpReq;
import com.phiture.erp.purchase.api.service.ErpPurchaseRequestApi;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author YH
 * @date 2025-05-13-14:22
 */

@Slf4j
@RequiredArgsConstructor
@Component("mrp_purchase_task")
public class MrpPurchaseTaskHandler extends BaseMrpTaskDispatchHandler implements MrpTaskDispatchHandler{
    private final ErpPurchaseRequestApi erpPurchaseRequestApi;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void taskExecute(MrpTaskDispatchDTO dto) {
        // 获取MRP明细
        List<ErpMaterialRequirementPlanItemsDO> items = getErpMaterialRequirementPlanItems(dto.getMrpItemIds());
        List<CreatePurchaseRequestFromMrpReq> reqList = ErpMaterialRequirementPlanConvertMapper.INSTANCE.toCreatePurchaseRequestFromMrpReqList(items);
        erpPurchaseRequestApi.createPurchaseRequestFromMrp(reqList, dto.getBatchMode());
        backfillItemsStatus(dto.getMrpItemIds());
    }
}
